package com.example.uupaymuch.controller.channel.uupay;

import com.example.uupaymuch.controller.health.UUPayAuthHelper;
import com.example.uupaymuch.controller.notice.uupay.UuPayComponent;
import com.example.uupaymuch.controller.notice.wudi.PayComponent;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Slf4j
@RestController
public class UupayPayOrderController {

    @Autowired
    private UuPayComponent uuPayComponent;
    @Autowired
    private UUPayAuthHelper uuPayAuthHelper;

    private static final String HMAC_SECRET = "yLTJnhYp6Plr5eFhHi96EVF3j1BS9Nj1ugLbZf6bXb7NxmqPfVqCfE2bMhUv5ZwLzLYW31BRZMpK5AIPZLNPXWaQOe6OnqwgW2UiZV5tdAuapzbR6RkFiYtRDEHkdipN"; // 从安全配置中获取

    private final Random random = new Random();
    private final double successProbability = 0.5; // 50% 的成功概率

    @PostMapping("/api/v1/pay/brazil/order")
    public ResponseEntity<Map<String, Object>> createTransaction(@RequestBody Map<String, Object> request) {
        // 生成一个随机数，并与成功概率进行比较
        boolean isSuccess = random.nextDouble() < successProbability;

        // 模拟入参
        String mchNo = request.get("mchNo") != null ? request.get("mchNo").toString() : "";
        String mchOrderNo = request.get("mchOrderNo") != null ? request.get("mchOrderNo").toString() : "";
        String amount = request.get("amount") != null ? request.get("amount").toString() : "";
        String subject = request.get("subject") != null ? request.get("subject").toString() : "";
        String body = request.get("body") != null ? request.get("body").toString() : "";
        String currency = request.get("currency") != null ? request.get("currency").toString() : "";
        String notifyUrl = request.get("notifyUrl") != null ? request.get("notifyUrl").toString() : "";
        String debtorName = request.get("debtorName") != null ? request.get("debtorName").toString() : "";
        String debtorDocumentNumber = request.get("debtorDocumentNumber") != null ? request.get("debtorDocumentNumber").toString() : "";
        String reqTime = String.valueOf(request.get("reqTime") != null ? request.get("reqTime").toString() : "");
        String needExtParam = request.get("needExtParam") != null ? request.get("needExtParam").toString() : "";
        String sign = request.get("sign") != null ? request.get("sign").toString() : "";

        long randomId = 10000000000L + (long) (random.nextDouble() * 90000000000L);
        String payOrderId = "WD" + randomId + mchNo;

        // 创建主响应 Map
        Map<String, Object> response = new HashMap<>();
        response.put("code", 0);
        response.put("msg", "SUCCESS");

        // 创建 data Map
        Map<String, Object> data = new HashMap<>();
        data.put("payOrderId", payOrderId);
        data.put("mchOrderNo", mchOrderNo);
        data.put("orderState", 1);
        data.put("payDataType", null);
        data.put("payData", null);
        data.put("payUrl", "https://urlpay.66u6.com?data=aWtz...");
        data.put("qrCode", "00020101021226810014br.gov.bcb.pix2559api-h.fakebank.com.br/api/cob/b5246aa1155b4c378ecc22c9c2dee3f65204000053039865802BR5913SUPERPAGAMENTOS LTDA6009SAO PAULO62070503***630442F4");
        data.put("errCode", null);
        data.put("errMsg", null);

        // 创建 channelRetMsg Map
        Map<String, Object> channelRetMsg = new HashMap<>();
        channelRetMsg.put("channelState", "WAITING");
        channelRetMsg.put("channelOrderId", "WD1727691281075M1727338282RDU1B");
        channelRetMsg.put("channelUserId", null);
        channelRetMsg.put("channelErrCode", null);
        channelRetMsg.put("channelErrMsg", null);
        channelRetMsg.put("channelAttach", null);
        channelRetMsg.put("channelOriginResponse", null);
        channelRetMsg.put("responseEntity", null);
        channelRetMsg.put("needQuery", false);

        // 将 channelRetMsg 放入 data Map
        data.put("channelRetMsg", channelRetMsg);

        // 将 data 放入主响应 Map
        response.put("data", data);

        // 添加签名
        response.put("sign", sign);

        // 回调
        Map<String, Object> noticeMap = new HashMap<>();
        noticeMap.put("payOrderId", payOrderId);
        noticeMap.put("mchNo", mchOrderNo);
        noticeMap.put("mchOrderNo", payOrderId);
        noticeMap.put("amount", amount);
        noticeMap.put("currency", currency);
        noticeMap.put("state", 2);
        noticeMap.put("subject", payOrderId);
        noticeMap.put("body", payOrderId);
        noticeMap.put("createdAt", System.currentTimeMillis());
        noticeMap.put("successTime", System.currentTimeMillis());
        noticeMap.put("reqTime", reqTime);
        String noticeSign = uuPayAuthHelper.assemblySign(noticeMap, HMAC_SECRET);
        noticeMap.put("sign", noticeSign);
        uuPayComponent.doNotifyPay(noticeMap, notifyUrl);

        // 立即返回响应（可以选择返回一个临时的响应）
        return ResponseEntity.ok(response);
    }



}
